const errorType = require("../constants/error-types");

function errorHandle(error, ctx) {
  let status, message;
  switch (error.message) {
    case errorType.NAME_OR_PASSWORD_AND_EMAIL_IS_REQUIRED:
      status = 400; // Bad Request
      message = "用户名 密码 邮箱是不能为空!";
      break;
    case errorType.USER_ALREADY_EXISTS:
      status = 409; // Conflict
      message = "用户名已存在~";
      break;
    case errorType.NAME_AND_PASSWORD_IS_REQUIRED:
      status = 400; // Conflict
      message = "用户名或密码不能为空~";
      break;
    case errorType.USER_IS_NOT_EXISTS:
      status = 400; // Conflict
      message = "用户不存在~";
      break;
    case errorType.PASSWORD_IS_INCORRECT:
      status = 400; // Conflict
      message = "密码不正确~";
      break;
    case errorType.EMAIL_IS_EXISTS:
      status = 400;
      message = "邮箱已注册!"; // Conflict
      break;
    default:
      status = 404;
      message = "NOT FOUND";
  }

  // ctx.status = status;
  // ctx.body = message;
  ctx.response.body = {
    status,
    message,
  };
}

module.exports = errorHandle;
